#if !defined(IMGLIB_WINIMAGE_H)
#define IMGLIB_WINIMAGE_H

#include "imglib/image.h"
#include "imglib/color.h"
#include "imglib/bitmap.h"
#include "imglib/winbmp.h"

#include <windows.h>

namespace img {

void DrawImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, const image< BGRA<unsigned char> > & img);
void DrawImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, const image< BGR<unsigned char> > & img);

void StretchImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, const image< BGRA<unsigned char> > & img, long ROP = SRCCOPY);
void StretchImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, const image< BGR<unsigned char> > & img, long ROP = SRCCOPY);

void BlendImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, const image< BGRA<unsigned char> > & img, unsigned char alpha);
void BlendImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, const image< BGRA<unsigned char> > & img);
void BlendImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, const image< BGR<unsigned char> > & img, unsigned char alpha);


template <class I> 
void StretchMaskedImage(HDC hDC, int xd, int yd, int wd, int hd, int xs, int ys, int ws, int hs, const I & img, const bitmap & mask)
{
	HDC tmpDC = CreateCompatibleDC(hDC);
	HBITMAP tmpBmp = CreateCompatibleBitmap(hDC, ws, hs);
	HGDIOBJ oldBmp = SelectObject(tmpDC, tmpBmp);

	RECT r;
	r.left = r.top = 0;
	r.right = ws;
	r.bottom = hs;	  

	FillRect(tmpDC, &r, (HBRUSH) GetStockObject(BLACK_BRUSH));

	COLORREF white = 0xFFFFFF, black = 0;

	DrawImage(tmpDC, 0, 0, ws, hs, xs, ys, img);
	StretchBitmap(tmpDC, 0, 0, ws, hs, xs, ys, ws, hs, mask, white, black, SRCAND);

	StretchBitmap(hDC, xd, yd, wd, hd, xs, ys, ws, hs, mask, black, white, SRCAND);

	StretchBlt(hDC, xd, yd, wd, hd, tmpDC, 0, 0, ws, hs, SRCPAINT);

	//StretchBlt(hDC, xd, yd, wd, hd, tmpDC, 0, 0, ws, hs, SRCCOPY);

	SelectObject(tmpDC, oldBmp);
	DeleteObject(tmpBmp);
	DeleteDC(tmpDC);
}

} // img

#endif // IMGLIB_WINIMAGE_H
